<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

require('echarts/theme/macarons') // echarts theme

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    chartData: {
      type: Array,
      required: true
    },
    typeName: {
      type: Array,
      required: true
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');
      let chartData = this.chartData;
      if (chartData.length > 0) {
        this.setOptions(this.chartData)
      }
    },
    setOptions({expectedData, actualData} = {}) {
      let chartData = this.chartData;
      let typeName = this.typeName;
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: "{b} <br/>{c} 元<br/> {d}%"
        },
        /*legend: {
          left: 'center',
          bottom: '10',
          // data: typeName
        },*/
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: '50%',
            data: chartData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
          /*{
            name: chartData[0].name,
            type: 'pie',
            roseType: 'radius',
            radius: "50%",
            center: ['50%', '38%'],
            data: chartData[0].data,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          },
          {
            name: chartData[1].name,
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: chartData[1].data,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          },
          {
            name: chartData[2].name,
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: chartData[2].data,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }*/
        ]
      });
    }
  }
}
</script>
